作者:manassatromble | 来源:互联网 | 2023-09-24 15:10
Withsomebackgroundonfunctionsinplaceandtheterminallogicalexactitudeestablished,let'sg
With some background on functions in place and the terminal logical exactitude established, let's get back to organizing the code in our words module into function.
We'll move all the code except the import statement, into a function called fetch words. You do that simply by adding the def statement and indenting the code below it by one extra level. Save the module, start a fresh Python REPL, and import your module again with import words. The module imports, but now the words are not fetched until we call the fetch_words function with words.fetch_words. This use of the dot is called qualifying the function name with the module name. Alternatively, we can import are specific function using a different form of the import statement, from words import fetch_words. Having imported the fetch_words function directly into our REPL session, which is itself a module, we can invoke fetch_words using its unqualified name. So far, so good.
But what happens when we try to run our module directly from the operating system shell? Exit from the REPL with Ctrl+D from Mac or Linux, or Ctrl+Z for Windows, and run Python, passing the module file name. No words are printed, that's because all the module does now is define a function and then exit. The function is never called.
What we'd prefer is that the module actually print something when we execute it. To make a module from which we can usefully import functions into the REPL and which can be run as a script, we need to learn a new Python idiom. As we mentioned earlier, one of the specially named variables is called dunder name, and it gives us the means to detect whether our module has been run as a script or imported into another module or the REPL. To see how, add print dunder name at the end of your module, outside of the fetch_words function. Let's import the modified words module back into the REPL with import words. We can see that when imported, dunder name does indeed evaluate to the module's name. As a brief aside, if you import the module again in the same REPL, the print statement will not be executed. Module code is only executed once on first import. Now let's try running the module as a script from the operating system shell with Python 3 words.py. Now the special dunder name variable is equal to the string dunder main, which is also delimited by double underscores. That is, Python sets the value of dunder name differently, depending on how our module is being used. The key idea we're introducing here is that our module can use this behavior to decide how it should behave. We replaced the print statement with an if statement, which tests the value of dunder name. If dunder name is equal to the string dunder main, we execute our function. On the other hand, if dunder name is not equal to dunder main, the module knows it's being imported into another module, not executed, and so only defines the fetch_words function without executing it. We can now safely import our module without unduly executing our function, and we can usefully run our module as a script.
于是,我们可以用这个知识来限定某些代码的执行,如果是直接运行其所在的py文件,则代码执行,如果是其他py文件导入当前py文件并运行,则代码不执行